package wikibooks;

import java.util.ArrayList;

/**
 * Contains settings related to the conversion of wikibooks, such as book title
 * and chapter names. Used in <code>WikibookConverter</code>. Create with
 * <code>new BookSettings(String title, boolean generateAllFiles)</code>. If
 * necessary, change settings using public fields and methods.
 */
public class BookSettings {

    private String title = "Wikibook",  author = "Wikibooks contributors",  path,  chapterListLocation;
    /** The wiki server URL */
    private String server = "en.wikibooks.org";
    /** The main directory where the index.php file is stored on the wiki server */
    private String mainDir = "/w/";
    /** Book language */
    private String language = "en";
    // {"filename","Chapter Title", "Short Title"}
    private ArrayList<String[]> chapters = new ArrayList<String[]>();
    public boolean generateMainFile,  generateTitlePage,  generateStyle,  generateDocInfo;
    public boolean generateKileProjectFile,  generateWikiPage,  generateChapters,  generateAuthorsList;
    public boolean generateChapterLinks;
    public boolean useTitlePageLogo,  getImages,  createChaptersFromFile;
    private String displayTitle;
    /** name of input file when converter is used in single file mode. By default null. */
    public String inputFile = null;
    /** name of output file when converter is used in single file mode. By default <tt>out.tex</tt>. */
    public String outputFile = "out.tex";
    /** name of single article to be translated; if not null, switches converter to single article mode. */
    public String article;

    public String getServer() {
        return server;
    }

    public void setServer(String serverURL) {
        if (serverURL == null){
            return;
        }
        this.server = serverURL;
    }

    /**
     * Returns the main WIKI directory on the selected {@link #server}.
     * @return the path to the main wiki directory on the selected server (including start and end slashes)
     */
    public String getMainDir(){
        return mainDir;
    }

    /**
     * Set up the main WIKI directory on the selected {@link #server}
     * @param dirPath path to the main wiki directory
     */
    void setMainDir(String dirPath) {
        if (dirPath == null){
            return;
        }
        if (dirPath.charAt(0) != '/'){
            dirPath = "/" + dirPath;
        }
        if (dirPath.charAt(dirPath.length()-1) != '/'){
            dirPath += '/';
        }
        mainDir = dirPath;
    }

    /**
     * returns two-letter code of the language this book is written in.
     * Currently only "en" and "pl" are supported.
     * @return two-letter string
     */
    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        if (language != null){
            this.language = language;
        }
    }

    public BookSettings(String title) {
        this(title, false, (String[]) null);
    }

    public BookSettings(String title, boolean generateAllFiles) {
        this(title, generateAllFiles, (String[]) null);
    }

    public BookSettings(String title, String... chapters) {
        this(title, false, chapters);
    }

    public BookSettings(String title, boolean generateAllFiles, String... chapters) {
        setTitle(title);
        generateAllFiles(generateAllFiles);
        addChapters(chapters);

        path = "";
        chapterListLocation = title;
    }

    public void setTitle(String title) {
        if (title != null) {
            this.title = title;
        }
        if (this.displayTitle == null) {
            this.displayTitle = title;
        }
    }

    public String title() {
        return title;
    }

    public void setDisplayTitle(String title) {
        this.displayTitle = title;
    }

    public String getDisplayTitle() {
        return displayTitle;
    }

    public void setAuthor(String author) {
        if (author != null) {
            this.author = author;
        }
    }

    public String author() {
        return author;
    }

    public void setPath(String path) {
        if (path != null) {
            this.path = path;
        }
    }

    public String path() {
        return path;
    }

    public void setChapterListLocation(String chapterListLocation) {
        if (chapterListLocation != null) {
            this.chapterListLocation = chapterListLocation;
        }
    }

    public String chapterListLocation() {
        return chapterListLocation;
    }

    /**
     * This adds an explicitly specified chapter (from the command-line) to the list of chapters.
     * Short title is set to empty string as it is not given
     * @param filename the output .tex / input .txt (if --ccff) file name
     * @param chapter the name of the chapter
     */
    public void addChapterFromFile(String filename, String chapter) {
        if (chapter != null && filename != null) {
            this.chapters.add(new String[]{filename, chapter, ""});
        }
    }

    /**
     * Adds a chapter to the chapter list. If short title equals chapter title, short
     * title is set to empty string.
     * @param chapter title of the chapter
     * @param shortTitle short title of chapter (or empty string if none exists)
     */
    public void addChapter(String chapter, String shortTitle) {
        chapter = chapter.trim();
        shortTitle = shortTitle.trim();
        if (shortTitle.equals(chapter)) {
            shortTitle = "";
        }

        String filename = Util.toSafeFilename(chapter);
        this.chapters.add(new String[]{filename, chapter, shortTitle});
    }

    public void addChapters(String... chapters) {
        if (chapters != null) {
            for (String chapter : chapters) {
                addChapter(chapter, "");
            }
        }
    }

    /**
     * returns list of book chapters
     * @return list of arrays [filename without .tex extension,chapter title,shorter title]
     * where shorter title can be empty string
     */
    public ArrayList<String[]> chapters() {
        return new ArrayList<String[]>(chapters);
    }

    /**
     * clears (makes empty) list of book chapters.
     */
    public void clearChapters() {
        chapters.clear();
    }

    public void generateAllFiles(boolean generateAllFiles) {
        if (generateAllFiles) {
            generateMainFile = true;
            generateTitlePage = true;
            generateStyle = true;
            generateDocInfo = true;
            generateKileProjectFile = true;
            generateWikiPage = true;
            generateChapters = true;
            generateAuthorsList = true;
            useTitlePageLogo = true;
            getImages = true;
        }
    }
    public boolean readTocFromFile = false;

}
 
